package com.pethospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pethospital.dto.ReviewDTO;
import com.pethospital.entity.Appointment;
import com.pethospital.entity.Review;
import com.pethospital.exception.BusinessException;
import com.pethospital.mapper.AppointmentMapper;
import com.pethospital.mapper.ReviewMapper;
import com.pethospital.service.HospitalInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 医院信息服务实现类
 */
@Slf4j
@Service
public class HospitalInfoServiceImpl implements HospitalInfoService {

    @Value("${pet-hospital.name:宠物医院}")
    private String hospitalName;

    @Value("${pet-hospital.address:北京市海淀区中关村大街1号}")
    private String hospitalAddress;

    @Value("${pet-hospital.phone:010-12345678}")
    private String hospitalPhone;

    @Value("${pet-hospital.introduction:全面的宠物医疗服务，专业的医生团队}")
    private String hospitalIntroduction;

    @Resource
    private ReviewMapper reviewMapper;

    @Resource
    private AppointmentMapper appointmentMapper;

    @Override
    public Map<String, Object> getHospitalInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("name", hospitalName);
        info.put("address", hospitalAddress);
        info.put("phone", hospitalPhone);
        info.put("introduction", hospitalIntroduction);
        info.put("openingHours", "周一至周日 9:00-18:00");
        return info;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addReview(ReviewDTO reviewDTO, Long userId) {
        // 验证预约是否存在且属于当前用户
        Appointment appointment = appointmentMapper.selectById(reviewDTO.getAppointmentId());
        if (appointment == null) {
            throw new BusinessException("预约不存在");
        }
        if (!appointment.getUserId().equals(userId)) {
            throw new BusinessException("无权对此预约进行评价");
        }
        if (appointment.getStatus() != 2) {
            throw new BusinessException("只能对已完成的预约进行评价");
        }

        // 验证医护人员ID是否与预约中的一致
        if (!appointment.getStaffId().equals(reviewDTO.getStaffId())) {
            throw new BusinessException("医护人员ID与预约不匹配");
        }

        // 检查是否已评价
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getAppointmentId, reviewDTO.getAppointmentId());
        if (reviewMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("该预约已评价");
        }

        // 创建评价
        Review review = new Review();
        review.setUserId(userId);
        review.setStaffId(reviewDTO.getStaffId());
        review.setAppointmentId(reviewDTO.getAppointmentId());
        review.setScore(reviewDTO.getScore());
        review.setContent(reviewDTO.getContent());
        review.setCreateTime(LocalDateTime.now());
        review.setUpdateTime(LocalDateTime.now());
        reviewMapper.insert(review);
        return review.getId();
    }

    @Override
    public Page<Review> getStaffReviews(Long staffId, int page, int size) {
        Page<Review> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getStaffId, staffId)
                    .orderByDesc(Review::getCreateTime);
        return reviewMapper.selectPage(pageParam, queryWrapper);
    }
} 